#!/bin/bash

value1=10
echo $value1

value1=`date`
echo $value1

value=$(date +%y%m%d)
echo $value

# ls -al /usr/bin  > log.$value

# 使用[]进行数学运算

num1=100
num2=32
minResult=$[$num1/$num2]
echo $minResult

var1=100
var2=45
var3=$[$var1 / $var2]
echo The final result is $var3

# 使用expr进行数学运算
echo "======="
echo "使用expr进行数学运算, 表达式中的特殊字符需要使用转义"
result=$(expr $var1 \/ $var2)
echo "\$(expr \$var1 \/ \$var2) = $result"

# 使用bc进行数学运算
result=$(echo "scale=4; $var1 / $var2" | bc)
echo "\$(echo \"scale=4; \$var1 / \$var2\" | bc) = $result"

result=$(echo "scale=4; 3.55 / 1.33" | bc)
echo "\$(echo \"scale=4; 3.55 / 1.33\" | bc) = ${result}"

var3=234
var4=532

echo "======="
echo "使用bc结合EOF进行数学运算"
# 使用bc结合EOF进行数学运算
var5=$(bc<<EOF
scale = 4
num1 = ($var1/$var2)
num2 = ($var3/$var4)
num1 + num2
EOF
)
echo "var5=${var5}"

# 结果化命令if-else的使用

# 命令返回值为0执行then
if pwd 
then
	echo "pwd success"
fi

# if 条件判断使用test
echo "======="
echo "\$var1 > \$var2  ！！！！ > 会被解释为重定向，所有吃法不可用"
if test $var1 \> $var2
then
	echo "var1 is greater than var2";
else
	echo "var1 is not greater than var2";
fi

echo "======="
echo "[ \$var1 \> \$var2 ] 这种方式在指定脚本为 /bin/bash 时可用，/bin/zsh 不可用"
if [ $var1 \> $var2 ]
then
	echo "var1 is greater than var2";
else
	echo "var1 is not greater than var2";
fi

echo "======="
echo "\$var1 -lt \$var2"
if test $var1 -lt $var2
then
	echo "var1 is less than var2";
else
	echo "var1 is not less than var2";
fi

echo "======="
echo "[ \$var1 -gt \$var2 ]"
# [ $var1 -gt $var2 ] [] 两边需要流出空格
if [ $var1 -gt $var2 ]; then
	#statements
	echo "var1 is greater than var2";
else
	echo "var1 is not greater than var2";
fi

echo "======="
echo "[ \$var1 -eq \$var2 ]"
if [ $var1 -eq $var2 ]; then
	echo "var1 is equal to var2";
else
	echo "var1 is not equal to var2";
fi


## 字符串的比较
echo "=======字符串的比较"
echo "[ \$str1 \> \$str2 ]"
str1=baseketball
str2=football
if [ $str1 \> $str2 ]; 
then
	echo $str1 greater than $str2
else
	echo $str1 not greater than $str2
fi 


echo "======="
echo "if嵌套的例子"
# if嵌套的例子
testUser=aron
if grep $testUser /etc/passwd 
then
	echo "user $testUser is in this system"	
	if ls /User$testUser; then
		echo "user $testUser has home folder"	
	else
		echo "However user $testUser has not home folder"
	fi
else
	echo "user $testUser is not in this system"	
	if ls /User${testUser}; then
		echo "user ${testUser} has home folder"	
	else
		echo "However user ${testUser} has not home folder"
	fi
fi

echo "文件比较"
# -d file	检查file是否存在并是一个目录 
# -e file	检查file是否存在 
# -f file	检查file是否存在并是一个文件 
# -r file	检查file是否存在并可读 
# -s file	检查file是否存在并非空 
# -w file	检查file是否存在并可写 
# -x file	检查file是否存在并可执行 
# -O file	检查file是否存在并属当前用户所有 
# -G file	检查file是否存在并且默认组与当前用户相同 
# file1 -nt file2 file1 -ot file2 	检查file1是否比file2新 

if [ -d $HOME ] && [ -w $HOME/testing ] 
then
   echo "The file exists and you can write to it"
else
   echo "I cannot write to the file"
fi


echo "======="
echo "(( \$num1 & \$num2 ))"
num1=16
num2=32
if (( $num1 & $num2 ))
then
echo "XXX"
fi

echo "======="
echo "(( \$num1 && \$num2 ))"
if (( $num1 && $num2 ))
then
echo "OOO"
fi

echo "======="
echo "(( \$val1 ** 2 > 90 )) "
# using double parenthesis #
val1=10
if (( $val1 ** 2 > 90 )) 
then
    (( val2 = $val1 ** 2 ))
    echo "The square of $val1 is $val2"
fi


if [[ $USER == r* ]]
then
	echo "Hello $USER"
else 
	echo "Sorry, I do not know you"
fi

echo "======="
echo "[[ \$val1 > 10 ]]"
if [[ $val1 > 10 ]]; then
	echo "\$val1 > 10";
else
	echo "\$val1 <= 10"
fi

echo "======="
echo "[[ \$val1 > \$val2 ]] [[  ]] 里面的表达式可以不用转义，这里和 [  ] 的使用方法有差别"
if [[ $val1 > $val2 ]]; then
	echo "\$val1 > \$val2";
else
	echo "\$val1 <= \$val2"
fi


echo "======="
echo "case $USER in"
case $USER in
rich | barbara)
       echo "Welcome, $USER"
       echo "Please enjoy your visit";;
    testing)
      echo "Special testing account";;
    aron)
       echo "Do not forget to log off when you're done";;
    *)
       echo "Sorry, you are not allowed here";;
esac
